package com.example.lemonservice.module.device.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lemoncommon.enums.ErrorEnum;
import com.example.lemoncommon.exception.ServiceException;
import com.example.lemonservice.entitiy.Device;
import com.example.lemonservice.entitiy.dto.UpdateDeviceDataDTO;
import com.example.lemonservice.mapper.DeviceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    final DeviceMapper deviceMapper;

    public DeviceServiceImpl(DeviceMapper deviceMapper) {
        this.deviceMapper = deviceMapper;
    }
    @Override
    public List<Device> getAllDevice() {
        return deviceMapper.selectList(null);
    }

    @Override
    public Boolean updateDeviceData(UpdateDeviceDataDTO dto) {
        Device device = deviceMapper.selectById(dto.getId());
        if (Objects.isNull(device)) {
            throw new ServiceException(ErrorEnum.DEVICE_NOT_EXIT.getErrCode(), ErrorEnum.DEVICE_NOT_EXIT.getErrMsg());
        }
        BeanUtils.copyProperties(dto, device);
        deviceMapper.updateById(device);
        return true;
    }
}
